package com.pantech.imnak0.translationhelper;

import java.io.File;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.pantech.imnak0.utility.FileUtility;

public class XLSCreator {
	public static final String SAVEFILE_NAME = File.separator + "translate_request.xls";
	private static final String EXCEL_FORM = "/translate_form.xls";
	private WritableWorkbook workBook;
	private WritableSheet sheet;

	private LinkedHashMap<String, LinkedHashSet<String>> hashMap;

	// String ID 의 criteria_row/criteria_column 값. 이 값을 기준으로 다른 번역 언어 등의 위치를 찾는다.
	private int criteria_row = 0;
	private int criteria_column = 0;

	private File savePath;

	public XLSCreator(File saveFile, LinkedHashMap<String, LinkedHashSet<String>> hashmapList) {
		this.savePath = saveFile;
		this.hashMap = hashmapList;
	}

	public void create() {
		try {
			initializeJXLInterface();

			setStringIDIndex();
			writeDataIntoExcel();

			closeJXLInterface();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * JXL interface를 오픈한다.
	 */
	private void initializeJXLInterface() throws Exception {
		File originalForm = FileUtility.createFileFromResource(getClass(), EXCEL_FORM);
		Workbook original = Workbook.getWorkbook(originalForm);

		if (savePath == null) {
			String resultFileName = new File(".").getCanonicalPath() + File.separator + "dropbox" + SAVEFILE_NAME;
			savePath = new File(resultFileName);
		} else {
			savePath = new File(savePath + SAVEFILE_NAME);
		}

		if (savePath.exists())
			savePath.delete();
		workBook = Workbook.createWorkbook(savePath, original);

		sheet = workBook.getSheet(1);
	}

	/**
	 * String ID column의 index를 확인하여 해당 변수값을 설정한다.
	 * 일반적인 상황이라면 D2 cell이 되겠지만, 혹시라도 excel format이 변경되는 경우를 대비하기 위함. 
	 */
	private void setStringIDIndex() {
		boolean flag = false;
		for (criteria_row = 0; criteria_row < 3; criteria_row++) {
			for (criteria_column = 0; criteria_column < 5; criteria_column++) {
				String content = sheet.getCell(criteria_column, criteria_row).getContents();
				if (content != null && content.equals("String ID")) {
					flag = true;
					break;
				}
			}

			if (flag)
				break;
		}
	}

	/**
	 * String ID 와 번역할 텍스트를 엑셀 문서에 기록한다.
	 */
	private void writeDataIntoExcel() throws Exception {
		Iterator<String> keyItr = hashMap.keySet().iterator();
		
		int i=0;
		while(keyItr.hasNext()) {
			String stringID = keyItr.next();
			
			LinkedHashSet<String>textSet = hashMap.get(stringID);
			Iterator<String> valueItr = textSet.iterator();
			
			while(valueItr.hasNext()) {
				String text = valueItr.next();
				
				Label key = new Label(criteria_column, criteria_row + 1 + i, stringID);
				Label value = new Label(criteria_column + 1, criteria_row + 1 + i, text);

				sheet.addCell(key);
				sheet.addCell(value);
				
				i++;
			}
		}
	}

	/**
	 * JXL Interface 를 close한다.
	 */
	private void closeJXLInterface() throws Exception {
		workBook.write();
		workBook.close();
	}
}
